home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / mac / macinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-02  |  70.0 KB  |  2,402 lines  |  [TEXT/KAHL]

  1. /* Initialization for the Mac interface to Xconq.
  2.    Copyright (C) 1992, 1993, 1994, 1995 Stanley T. Shebs.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING. */
  8.  
  9. #include "conq.h"
  10. #include "macconq.h"
  11.  
  12. extern Rect dragrect;
  13.  
  14. /* Limit on the number of check boxes and sliders in the variants dialog. */
  15.  
  16. #define MAXCHECKBOXES 7
  17. #define MAXSLIDERS 3
  18.  
  19. #define toggle_checkbox(ih) \
  20.   SetCtlValue((ControlHandle) (ih), !GetCtlValue((ControlHandle) (ih)))
  21.  
  22. /* (not quite right - come up with a better test) */
  23.  
  24. #define game_already_loaded() (numutypes > 0)
  25.  
  26. #define t_color_defined(t) (hasColorQD && tcolors != NULL && tcolors[t] != NULL)
  27.  
  28. extern int foundimagesfile;
  29. extern int allbedesigners;
  30.  
  31. /* Private functions. */
  32.  
  33. static pascal void draw_game_list(WindowPtr win, short ditem);
  34. static pascal void draw_game_blurb(WindowPtr win, short ditem);
  35. static pascal void draw_game_picture(WindowPtr win, short ditem);
  36. static void center_pict(PicHandle picture, Rect *rect);
  37. static Module *module_from_cell(Cell cell);
  38. static pascal Boolean filter_new_game(DialogPtr dialog, EventRecord *evt, short *itemhit);
  39. static void display_selected_game(void);
  40. static void select_game(void);
  41. static pascal void draw_variant_slider(WindowPtr win, short ditem);
  42. static pascal void draw_variant_help(WindowPtr win, short ditem);
  43. static void set_variant_item(int di, int flag, int flag2, int val);
  44. static void interpret_variants(void);
  45. static void implement_variants(void);
  46. static void special_xform(int x, int y, int *sxp, int *syp);
  47. static pascal void draw_world_picture(WindowPtr win, short ditem);
  48. static pascal Boolean filter_world_setup(DialogPtr dialog, EventRecord *evt, short *itemhit);
  49. static int world_shape_dialog(void);
  50. static int real_time_dialog(void);
  51. static int more_variants_dialog(void);
  52. static pascal void draw_player_setup_list(WindowPtr win, short ditem);
  53. static pascal void draw_player_setup_advantage(WindowPtr win, short ditem);
  54. static int adjust_advantage(Player *player, Side *side, int amt);
  55. static void select_player(int n);
  56. static int player_setup_dialog(void);
  57. static void set_player_setup_button_states(void);
  58. static void add_default_unit_image(ImageFamily *imf, int u, char *name);
  59. static void add_default_terrain_image(ImageFamily *imf, int t, char *name);
  60. static void add_default_emblem_image(ImageFamily *imf, int e, char *name);
  61.  
  62. /* Global variables. */
  63.  
  64. BitMap *bordbitmaps;
  65. BitMap *connbitmaps;
  66.  
  67. /* This is the dialog used to select a game to play. */
  68.  
  69. WindowPtr newgamewin = nil;
  70.  
  71. ListHandle newgamelist = nil;
  72.  
  73. TEHandle newgametext = nil;
  74.  
  75. PicHandle newgamepicture = nil;
  76.  
  77. static Module *selected_game = NULL;
  78.  
  79. /* True if any variants are available. */
  80.  
  81. int any_variants;
  82.  
  83. /* These are true when a standard variant is available to be chosen. */
  84.  
  85. int vary_world_seen;
  86. int vary_see_all;
  87. int vary_world;
  88. int vary_sequential;
  89. int vary_real_time;
  90.  
  91. /* This is the dialog used to set the size and shape of the world. */
  92.  
  93. DialogPtr world_shape_win = nil;
  94.  
  95. /* These are the values that will be used to set world geometry. */
  96.  
  97. long new_circumference;
  98. long new_width;
  99. long new_height;
  100. long new_latitude;
  101. long new_longitude;
  102. long new_scale;
  103.  
  104. Handle hexagon_icon;
  105. Handle cylinder_icon;
  106.  
  107. /* This is the dialog used to set realtime limits. */
  108.  
  109. DialogPtr real_time_win = nil;
  110.  
  111. long new_time_for_game;
  112. long new_time_per_side;
  113. long new_time_per_turn;
  114.  
  115. /* This is the dialog used to set more variants. */
  116.  
  117. DialogPtr more_variants_win = nil;
  118.  
  119. /* This is the variant-setting dialog. */
  120.  
  121. DialogPtr variants_win = nil;
  122.  
  123. int numcheckboxes;
  124. int numsliders;
  125.  
  126. Variant *checkboxes[MAXCHECKBOXES];
  127. Variant *sliders[MAXSLIDERS];
  128.  
  129. /* These are the new user-chosen values for variants. */
  130.  
  131. int new_seen;
  132. int new_seeall;
  133. int new_sequential;
  134.  
  135. Obj *variants;
  136.  
  137. /* This is the player setup dialog. */
  138.  
  139. WindowPtr playersetupwin = nil;
  140.  
  141. PicHandle updownpicture = nil;
  142. PicHandle updownpictureup = nil;
  143. PicHandle updownpicturedown = nil;
  144.  
  145. int playerh = 22;
  146. int playerbaseline = 14;
  147.  
  148. int selectedplayer = -1;
  149.  
  150. #define selected_a_player() (between(0, selectedplayer, numsides - 1))
  151.  
  152. #define selected_player()  \
  153.   (selected_a_player() ? assignments[selectedplayer].player : NULL)
  154.  
  155. #define selected_side() (selected_a_player() ? assignments[selectedplayer].side : NULL)
  156.  
  157. ImageFamily **uimages = NULL;
  158. ImageFamily **timages = NULL;
  159. ImageFamily **eimages = NULL;
  160.  
  161. ImageColor **tcolors = NULL;
  162.  
  163. Handle animationpatterns;
  164.  
  165. CursHandle readprogressors[NUMcParens];
  166. CursHandle progressors[NUMcSynth];
  167. CursHandle movecursors[NUMcMoves];
  168. CursHandle nomovecursor;
  169. CursHandle allmovecursor;
  170. CursHandle grayarrowcursor;
  171. CursHandle opencrosscursor;
  172. CursHandle firecursor;
  173. CursHandle watchcursor;
  174.  
  175. /* This value is the current progress cursor. */
  176.  
  177. int curcurs = 0;
  178.  
  179. /* The initialization progress dialog. */
  180.  
  181. DialogPtr progresswin = nil;
  182.  
  183. /* The current value of the percent progress. */
  184.  
  185. int progress;
  186.  
  187. /* The previous value of the percent progress. */
  188.  
  189. int lastprogress;
  190.  
  191. enum grays gridgray = whitegray;
  192. enum grays unseengray = whitegray;
  193. enum grays bggray = mediumgray;
  194.  
  195. RGBColor gridcolor;
  196. RGBColor unseencolor;
  197. RGBColor blackcolor;
  198.  
  199. int grid_matches_unseen = FALSE;
  200.  
  201. PicHandle dotdotdotpicture = nil;
  202.  
  203. int first_windows;
  204.  
  205. /* Set up any generic patterns that will be needed. */
  206.  
  207. void
  208. init_patterns()
  209. {
  210.     animationpatterns = GetResource('PAT#', 128);
  211.     /* We have to lock forever these because they will be used unpredictably. */
  212.     /* (should be done more intelligently) */
  213.     HLock(animationpatterns);
  214. }
  215.  
  216. /* Set up any generic icons that will be needed. */
  217.  
  218. void
  219. init_icons()
  220. {
  221.     hexagon_icon = GetResource('ICON', 128);
  222.     cylinder_icon = GetResource('ICON', 129);
  223. }
  224.  
  225. /* Set up the various cursors we'll be using. */
  226.  
  227. void
  228. init_cursors()
  229. {
  230.     int i;
  231.  
  232.     /* Get all the cursors used to indicate reader progress. */
  233.     for (i = 0; i < NUMcParens; ++i) {
  234.         readprogressors[i] = GetCursor(cParens1 + i);
  235.     }
  236.     /* Get all the cursors used to indicate synthesis progress. */
  237.     for (i = 0; i < NUMcSynth; ++i) {
  238.         progressors[i] = GetCursor(cSynth1 + i);
  239.     }
  240.     /* Get all the cursors indicating the move direction. */
  241.     for (i = 0; i < NUMcMoves; ++i) {
  242.         movecursors[i] = GetCursor(cMove1 + i);
  243.     }
  244.     /* Miscellaneous cursors. */
  245.     nomovecursor = GetCursor(cNoMove);
  246.     allmovecursor = GetCursor(cAllMove);
  247.     grayarrowcursor = GetCursor(cGrayArrow);
  248.     opencrosscursor = GetCursor(cOpenCross);
  249.     firecursor = GetCursor(138);
  250.     watchcursor = GetCursor(watchCursor);
  251.     /* Designer-related cursors will be done later, if needed at all. */
  252.  
  253.     dotdotdotpicture = (PicHandle) GetResource('PICT', 134);
  254. }
  255.  
  256. pascal Boolean
  257. filter_splash(DialogPtr dialog, EventRecord *evt, short *itemhit)
  258. {
  259.     char ch;
  260.  
  261.     /* Look for the right kind of event. */
  262.     switch (evt->what) {
  263.         case keyDown:
  264.             ch = evt->message & charCodeMask;
  265.             if (ch == 3 || ch == 13) {
  266.                 *itemhit = diSplashNew;
  267.                 return TRUE;
  268.             }
  269. #ifdef DEBUGGING
  270.             /* A secret way to get debugging at startup. */
  271.             /* (should have some sort of feedback?) */
  272.             if (ch == 'D') {
  273.                 toggle_debugging(&Debug);
  274.             }
  275.             if (ch == 'M') {
  276.                 toggle_debugging(&DebugM);
  277.             }
  278.             if (ch == 'G') {
  279.                 toggle_debugging(&DebugG);
  280.             }
  281. #endif
  282.             break;
  283.     }
  284.     return FALSE;
  285. }
  286.  
  287. /* Display the initial splash screen, and let the player choose New/Open/Connect/Quit. */
  288.  
  289. int
  290. do_splash_box()
  291. {
  292.     short ditem;
  293.     Str255 tmpstr;
  294.     WindowPtr win;
  295.     PicHandle pic;
  296.     short itemtype;  Handle itemhandle;  Rect itemrect;
  297.  
  298.     win = GetNewDialog(dSplash, NULL, (DialogPtr) -1L);
  299.     /* Fill in the kernel's version and copyright. */
  300.     GetDItem(win, diSplashVersion, &itemtype, &itemhandle, &itemrect);
  301.     c2p(version_string(), tmpstr);
  302.     SetIText(itemhandle, tmpstr);
  303.     GetDItem(win, diSplashCopyright, &itemtype, &itemhandle, &itemrect);
  304.     c2p(copyright_string(), tmpstr);
  305.     SetIText(itemhandle, tmpstr);
  306.     /* Substitute a color picture if possible. */
  307.     if (hasColorQD) {
  308.         GetDItem(win, diSplashPicture, &itemtype, &itemhandle, &itemrect);
  309.         pic = (PicHandle) GetResource('PICT', pSplashColor);
  310.         if (pic != nil) {
  311.             SetDItem(win, diSplashPicture, itemtype, (Handle) pic, &itemrect);
  312.         }
  313.     }
  314.     ShowWindow(win);
  315.     SelectWindow(win);
  316.     ModalDialog((ModalFilterProcPtr) filter_splash, &ditem);
  317.     /* We don't loop around here, just return the ditem and let caller decide what to do. */
  318.     DisposDialog(win);
  319.     return ditem;
  320. }
  321.  
  322. /* Dialog app-defined item callback that displays the list of possible games. */
  323.  
  324. pascal void
  325. draw_game_list(WindowPtr win, short ditem)
  326. {
  327.     short itemtype;  Handle itemhandle;  Rect itemrect;
  328.  
  329.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  330.     /* Draw the list of available games. */
  331.     LUpdate(newgamewin->visRgn, newgamelist);
  332.     /* Frame it nicely. */
  333.     --itemrect.left;
  334.     FrameRect(&itemrect);
  335. }
  336.  
  337. /* Dialog app-defined item callback to display the game description. */
  338.  
  339. pascal void
  340. draw_game_blurb(WindowPtr win, short ditem)
  341. {
  342.     GrafPtr oldport;
  343.     short itemtype;  Handle itemhandle;  Rect itemrect;
  344.  
  345.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  346.     /* Make sure the text is up-to-date. */
  347.     GetPort(&oldport);
  348.     SetPort(newgamewin);
  349.     TEUpdate(&itemrect, newgametext);    
  350.     SetPort(oldport);
  351.     /* Frame it. */
  352.     InsetRect(&itemrect, -1, -1);
  353.     FrameRect(&itemrect);
  354. }
  355.  
  356. /* Dialog app-defined item callback to display the game picture, if available. */
  357.  
  358. pascal void
  359. draw_game_picture(WindowPtr win, short ditem)
  360. {
  361.     RgnHandle tmprgn;
  362.     Rect cliprect;
  363.     short itemtype;  Handle itemhandle;  Rect itemrect;
  364.  
  365.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  366.     EraseRect(&itemrect);
  367.     if (newgamepicture != nil) {
  368.         tmprgn = NewRgn();
  369.         GetClip(tmprgn);
  370.         cliprect = itemrect;
  371.         ClipRect(&cliprect);
  372.         center_pict(newgamepicture, &itemrect);
  373.         DrawPicture(newgamepicture, &itemrect);
  374.         SetClip(tmprgn);
  375.         DisposeRgn(tmprgn);
  376.     }
  377. #ifdef DESIGNERS
  378.     /* A low-impact feedback that this game will launch directly into designing. */
  379.     if (allbedesigners) {
  380.         PenSize(3, 3);
  381.         itemrect.right -= 3;  itemrect.bottom -= 3;
  382.         FrameRect(&itemrect);
  383.         PenNormal();
  384.     }
  385. #endif /* DESIGNERS */
  386. }
  387.  
  388. /* Utility that figures out how to center a picture in a rectangle. */
  389.  
  390. void
  391. center_pict(PicHandle picture, Rect *rect)
  392. {
  393.     int picth, pictv;
  394.     Rect initrect, pictrect;
  395.  
  396.     initrect = *rect;
  397.     pictrect = (*picture)->picFrame;
  398.     picth = pictrect.right - pictrect.left;  pictv = pictrect.bottom - pictrect.top;
  399.     rect->left += (initrect.right - initrect.left) / 2 - picth / 2;
  400.     rect->top += (initrect.bottom - initrect.top) / 2 - pictv / 2;
  401.     rect->right = rect->left + picth;
  402.     rect->bottom = rect->top + pictv;
  403. }
  404.  
  405. static Module *
  406. module_from_cell(Cell cell)
  407. {
  408.     return (cell.v < numgames ? possible_games[cell.v] : NULL);
  409. }
  410.  
  411. /* This filter for the new game dialog just handles the game list, updating the
  412.    other items to reflect the currently selected game. */
  413.  
  414. int lastnewgameclick;
  415. Point lastnewgamemouse;
  416.  
  417. static pascal Boolean
  418. filter_new_game(DialogPtr dialog, EventRecord *evt, short *itemhit)
  419. {
  420.     GrafPtr oldport;
  421.     Point pt, origpt;
  422.     short ditem;
  423.     char ch;
  424.     short itemtype;  Handle itemhandle;  Rect itemrect;
  425.  
  426.     /* Look for the right kind of event. */
  427.     switch (evt->what) {
  428.         case mouseDown:
  429.             GetPort(&oldport);
  430.             SetPort(dialog);
  431.             pt = origpt = evt->where;
  432.             GlobalToLocal(&pt);
  433.             ditem = FindDItem(dialog, pt) + 1;
  434.             if (ditem == diNewGameList) {
  435.                 /* Loop around in list handling. */
  436.                 LClick(pt, evt->modifiers, newgamelist);
  437.                 /* We're finished clicking, show the results (if not in the list's scrollbar). */
  438.                 GetDItem(dialog, ditem, &itemtype, &itemhandle, &itemrect);
  439.                 if (pt.h < (itemrect.right - sbarwid)) {
  440.                     select_game();
  441.                     display_selected_game();
  442.                     SetPort(oldport);
  443.                     if (TickCount() - lastnewgameclick < GetDblTime()
  444.                         && between(-3, origpt.h - lastnewgamemouse.h, 3)
  445.                         && between(-3, origpt.v - lastnewgamemouse.v, 3)
  446.                         ) {
  447.                         *itemhit = diNewGameOK;
  448.                     } else {
  449.                         lastnewgameclick = TickCount();
  450.                         lastnewgamemouse = evt->where;
  451.                         *itemhit = diNewGameList;
  452.                     }
  453.                 }
  454.                 return TRUE;
  455.             } else {
  456.                 SetPort(oldport);
  457.                 return FALSE;
  458.             }
  459.             break;
  460.         case keyDown:
  461.             ch = evt->message & charCodeMask;
  462.             if (ch == 3 || ch == 13) {
  463.                 if (selected_game) {
  464.                     *itemhit = diNewGameOK;
  465.                     return TRUE;
  466.                 }
  467.             }
  468. #ifdef DESIGNERS
  469.             /* A secret way to get into designing at startup. */
  470.             if (ch == 'd') {
  471.                 allbedesigners = !allbedesigners;
  472.                 DrawDialog(newgamewin);
  473.             }
  474. #endif
  475. #ifdef DEBUGGING
  476.             /* A secret way to get debugging at startup. */
  477.             if (ch == 'D') {
  478.                 toggle_debugging(&Debug);
  479.                 DrawDialog(newgamewin);
  480.             }
  481.             if (ch == 'M') {
  482.                 toggle_debugging(&DebugM);
  483.                 DrawDialog(newgamewin);
  484.             }
  485.             if (ch == 'G') {
  486.                 toggle_debugging(&DebugG);
  487.                 DrawDialog(newgamewin);
  488.             }
  489. #endif
  490.             break;
  491.     }
  492.     return FALSE;
  493. }
  494.  
  495. /* Given a selected game, display assorted info about it - basically a preview so that
  496.    prospective players can see what they're getting into.  If no game has been selected,
  497.    then this routine clears the displays. */
  498.  
  499. static void
  500. display_selected_game()
  501. {
  502.     short itemtype;  Handle itemhandle;  Rect itemrect;
  503.     char *desc;
  504.     Str255 tmpstr;
  505.     PicHandle oldnewgamepicture;
  506.  
  507.     DGprintf("display selected game %s\n", module_desig(selected_game));
  508.     /* Set the blurb for the selected game. */
  509.     GetDItem(newgamewin, diNewGameBlurb, &itemtype, &itemhandle, &itemrect);
  510.     TESetSelect(0, 100000, newgametext);
  511.     TECut(newgametext);
  512.     if (selected_game /* && selected_game->complete */) {
  513.         if (selected_game->blurb) {
  514.             desc = selected_game->blurb;
  515.         } else {
  516.             desc = "??? experimental ???";
  517.         }
  518.     } else {
  519.         desc = "";
  520.     }
  521.     TESetText(desc, strlen(desc), newgametext);
  522.     TEUpdate(&itemrect, newgametext);
  523.     /* Load a picture if one can be found. */
  524.     /* (should look for picture explicitly named in a game-module slot first) */
  525.     oldnewgamepicture = newgamepicture;
  526.     newgamepicture = nil;
  527.     if (selected_game) {
  528.         sprintf(spbuf, "%s game", selected_game->name);
  529.         c2p(spbuf, tmpstr);
  530.         newgamepicture = (PicHandle) GetNamedResource('PICT', tmpstr);
  531.         if (newgamepicture == nil) {
  532.             sprintf(spbuf, "%s", selected_game->name);
  533.             c2p(spbuf, tmpstr);
  534.             newgamepicture = (PicHandle) GetNamedResource('PICT', tmpstr);
  535.         }
  536.     } else {
  537.         /* Nothing to display - how boring! */
  538.     }
  539.     /* Gray out the OK button if no game selected. */
  540.     GetDItem(newgamewin, diNewGameOK, &itemtype, &itemhandle, &itemrect);
  541.     HiliteControl((ControlHandle) itemhandle, (selected_game ? 0 : 255));
  542.     /* We have to force redraw to get the picture item updated. */
  543.     if (oldnewgamepicture != newgamepicture)
  544.       DrawDialog(newgamewin);
  545. }
  546.  
  547. /* This is a modal dialog from which the user selects a game and sets options. */
  548.  
  549. void
  550. new_game_dialog()
  551. {
  552.     int done = FALSE;
  553.     short ditem, i;
  554.     Point cellsize;
  555.     Cell tmpcell;
  556.     Rect listrect, destrect, viewrect;
  557.     Module *module;
  558.     char *gamename;
  559.     char tmpbuf[255];
  560.     short itemtype;  Handle itemhandle;  Rect itemrect;
  561.  
  562.     collect_possible_games();
  563.     if (newgamewin == nil) {
  564.         newgamewin = GetNewDialog(dNewGame, NULL, (DialogPtr) -1);
  565.         SetPort(newgamewin);
  566.         TextFont(newYork);
  567. /*        TextSize(10); */
  568.         /* Set up the app-defined item that lists games. */
  569.         GetDItem(newgamewin, diNewGameList, &itemtype, &itemhandle, &itemrect);
  570.         SetDItem(newgamewin, diNewGameList, itemtype, (Handle) draw_game_list, &itemrect);
  571.         SetPt(&cellsize, 0, 0);
  572.         listrect.top = 0;  listrect.left = 0;
  573.         listrect.bottom = 0;  listrect.right = 1;
  574.         itemrect.top += 1;
  575.         itemrect.bottom -= 1;  itemrect.right -= sbarwid + 1;
  576.         /* Create the list of games itself and fill it in. */
  577.         newgamelist = LNew(&itemrect, &listrect, cellsize, 0, newgamewin, 0,0,0,TRUE);
  578.         SetPt(&tmpcell, 0, 0);
  579.         for (i = 0; i < numgames; ++i) {
  580.             module = possible_games[i];
  581.             gamename = (module->title ? module->title : module->name);
  582.             sprintf(tmpbuf, "%s%s", (module->basemodulename ? "-  " : ""), gamename);
  583.             LAddRow(1, tmpcell.v, newgamelist);
  584.             LSetCell(tmpbuf, strlen(tmpbuf), tmpcell, newgamelist);
  585.             ++tmpcell.v;
  586.         }
  587.         GetDItem(newgamewin, diNewGameBlurb, &itemtype, &itemhandle, &itemrect);
  588.         SetDItem(newgamewin, diNewGameBlurb, itemtype, (Handle) draw_game_blurb, &itemrect);
  589.         destrect = itemrect;
  590.         viewrect = itemrect;
  591. /*        TextSize(10);  */
  592.         newgametext = TENew(&destrect, &viewrect);
  593.         GetDItem(newgamewin, diNewGamePicture, &itemtype, &itemhandle, &itemrect);
  594.         SetDItem(newgamewin, diNewGamePicture, itemtype, (Handle) draw_game_picture, &itemrect);
  595.         newgamepicture = nil;
  596.     }
  597.     update_new_game_list();
  598.     SetPt(&tmpcell, 0, 0);
  599.     LSetSelect(TRUE, tmpcell, newgamelist);  /* (do this only for new/changed list?) */
  600.     select_game();
  601.     display_selected_game();
  602.     SetCursor(&QD(arrow));
  603.     ShowWindow(newgamewin);
  604.     LDoDraw(1, newgamelist);
  605.     /* Loop around here until the player picks a game to play, or cancels. */
  606.     while (!done) {
  607.         draw_default_button(newgamewin, diNewGameOK);
  608.         ModalDialog((ModalFilterProcPtr) filter_new_game, &ditem);
  609.         switch (ditem) {
  610.             case diNewGameOK:
  611.                 /* Make this dialog disappear now, startup may take a long time. */
  612.                 HideWindow(newgamewin);
  613.                 if (start_new_game()) {
  614.                     done = TRUE;
  615.                 } else {
  616.                     /* If we failed to start the new game, just go around again. */
  617.                     ShowWindow(newgamewin);
  618.                     SelectWindow(newgamewin);
  619.                 }
  620.                 break;
  621.             case diNewGameCancel:
  622.                 done = TRUE;
  623.                 break;
  624.             default:
  625.                 break;
  626.         }
  627.     }
  628.     /* We're done, OK to throw away the dialog. */
  629.     DisposDialog(newgamewin);
  630.     newgamewin = nil;
  631.     /* (should release TEs and lists also) */
  632. }
  633.  
  634. /* Select a game from the list of possible games. */
  635.  
  636. static void
  637. select_game()
  638. {
  639.     Cell tmpcell;
  640.  
  641.     SetPt(&tmpcell, 0, 0);
  642.     if (game_already_loaded()) {
  643.         /* selected_game is already set correctly. */
  644.     } else if (LGetSelect(TRUE, &tmpcell, newgamelist)) {
  645.         selected_game = module_from_cell(tmpcell);
  646.     } else {
  647.         selected_game = NULL;
  648.     }
  649. }
  650.  
  651. /* When the new game OK button is hit, this will do the work of getting things started,
  652.    possibly pausing to ask about player/side setup. */
  653.  
  654. int
  655. start_new_game()
  656. {
  657.     if (selected_game == NULL) { /* Just in case... */
  658.         beep();
  659.         return FALSE;
  660.     }
  661.     if (!game_already_loaded()) {
  662.         /* Suck in the selected module.  This step cannot be undone. */
  663.         mainmodule = selected_game;
  664.         load_game_module(selected_game, TRUE);
  665.         /* Change cursor back, in case it was different during loading. */
  666.         SetCursor(&QD(arrow));
  667.         /* If the loaded game is not valid, we will get an alert somewhere in here,
  668.            and possibly bomb out if the player chooses not to continue. */
  669.         check_game_validity();
  670.     }
  671.     return launch_game();
  672. }
  673.  
  674. /* This routine is for when we end up back in the new game dialog,
  675.    after something has already been loaded. */
  676.  
  677. /* (could also include any game that has this one as a base module) */
  678.  
  679. static void
  680. update_new_game_list()
  681. {
  682.     char *gamename;
  683.     Point tmpcell;
  684.     
  685.     if (newgamelist != nil && game_already_loaded()) {
  686.         SetPt(&tmpcell, 0, 0);
  687.         /* Clear out the entire list. */
  688.         LDelRow(0, 0, newgamelist);
  689.         /* Add the loaded game back. */ 
  690.         LAddRow(1, tmpcell.v, newgamelist);
  691.         /* Find a name to paste into the list. */
  692.         gamename = (selected_game->title ? selected_game->title : selected_game->name);
  693.         /* Set and select the one cell. */
  694.         LSetCell(gamename, strlen(gamename), tmpcell, newgamelist);
  695.         LSetSelect(TRUE, tmpcell, newgamelist);
  696.     }
  697. }
  698.  
  699. static pascal void
  700. draw_variant_slider(WindowPtr win, short ditem)
  701. {
  702.     Str255 tmpstr;
  703.     short itemtype;  Handle itemhandle;  Rect itemrect;
  704.  
  705.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  706.     /* Always frame. */
  707.     FrameRect(&itemrect);
  708.     if (between(diVariantsFirstSlider, ditem, diVariantsFirstSlider + numsliders - 1)) {
  709.         MoveTo(itemrect.left + 2, itemrect.top + 10);
  710.         c2p(sliders[ditem - diVariantsFirstSlider]->name, tmpstr);
  711.         DrawString(tmpstr);
  712.     } else {
  713.         gray_out_rect(&itemrect);
  714.     }
  715. }
  716.  
  717. static pascal void
  718. draw_variant_help(win, ditem)
  719. WindowPtr win;
  720. short ditem;
  721. {
  722.     short itemtype;  Handle itemhandle;  Rect itemrect;
  723.  
  724.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  725.     /* Always frame. */
  726.     FrameRect(&itemrect);
  727. }
  728.  
  729. /* The variants dialog basically handles all the player-settable options defined
  730.    by a game. */
  731.  
  732. static int
  733. variants_dialog()
  734. {
  735.     int i, done = FALSE, changed = FALSE;
  736.     char *gamename;
  737.     Str255 tmpstr;
  738.     short ditem;
  739.     short itemtype;  Handle itemhandle;  Rect itemrect;
  740.  
  741.     if (selected_game == NULL)
  742.       init_error("variants on a nonexistent game?");
  743.     interpret_variants();
  744.     /* If no variants defined, get out of here. */
  745.     if (!any_variants)
  746.       return TRUE;
  747.     if (variants_win == nil) {
  748.         variants_win = GetNewDialog(dVariants, NULL, (DialogPtr) -1L);
  749.         /* Set the title of this dialog appropriately. */
  750.         gamename = (selected_game->title ? selected_game->title : selected_game->name);
  751.         sprintf(spbuf, "Variants for \"%s\"", gamename);
  752.         c2p(spbuf, tmpstr);
  753.         GetDItem(variants_win, diVariantsText, &itemtype, &itemhandle, &itemrect);
  754.         SetIText(itemhandle, tmpstr);
  755.         /* Give all the slider app items the same proc. */
  756.         for (i = diVariantsFirstSlider; i < (diVariantsFirstSlider + MAXSLIDERS); ++i) {
  757.             GetDItem(variants_win, i, &itemtype, &itemhandle, &itemrect);
  758.             SetDItem(variants_win, i, itemtype, (Handle) draw_variant_slider, &itemrect);
  759.         }
  760.         GetDItem(variants_win, diVariantsHelp, &itemtype, &itemhandle, &itemrect);
  761.         SetDItem(variants_win, diVariantsHelp, itemtype, (Handle) draw_variant_help, &itemrect);
  762.     }
  763.     /* Display/default the standard variants appropriately. */
  764.     set_variant_item(diVariantsWorldSeen, vary_world_seen, TRUE, new_seen);
  765.     set_variant_item(diVariantsSeeAll, vary_see_all, TRUE, new_seeall);
  766.     set_variant_item(diVariantsSequential, vary_sequential, TRUE, new_sequential);
  767.     set_variant_item(diVariantsWorldSize, vary_world, FALSE, 0);
  768.     set_variant_item(diVariantsRealTime, vary_real_time, FALSE, 0);
  769.     set_variant_item(diVariantsMoreVariants, /* more_variants */ FALSE, FALSE, 0);
  770.     /* For each random checkbox used, give it a title and make it displayable,
  771.        otherwise gray it. */
  772.     for (i = 0; i < MAXCHECKBOXES; ++i) {
  773.         GetDItem(variants_win, diVariantsFirstCheckBox + i, &itemtype, &itemhandle, &itemrect);
  774.         if (i < numcheckboxes) {
  775.             c2p(checkboxes[i]->name, tmpstr);
  776.             SetCTitle((ControlHandle) itemhandle, tmpstr);
  777.             ShowControl((ControlHandle) itemhandle);
  778.             /* should set its initial state based on variant's default */
  779.         } else {
  780.             sprintf(spbuf, "Unused");
  781.             c2p(spbuf, tmpstr);
  782.             SetCTitle((ControlHandle) itemhandle, tmpstr);
  783.             ShowControl((ControlHandle) itemhandle);
  784.             HiliteControl((ControlHandle) itemhandle, 255);
  785.         }
  786.     }
  787.     ShowWindow(variants_win);
  788.     while (!done) {
  789.         draw_default_button(variants_win, diVariantsOK);
  790.         ModalDialog(NULL, &ditem);
  791.         switch (ditem) {
  792.             case diVariantsOK:
  793.                 implement_variants();
  794.                 changed = TRUE;
  795.                 /* Fall through to next case. */
  796.             case diVariantsCancel:
  797.                 done = TRUE;
  798.                 break;
  799.             case diVariantsWorldSeen:
  800.             case diVariantsSeeAll:
  801.             case diVariantsSequential:
  802.                 /* Toggle check boxes. */
  803.                 GetDItem(variants_win, ditem, &itemtype, &itemhandle, &itemrect);
  804.                 toggle_checkbox(itemhandle);
  805.                 break;
  806.             case diVariantsWorldSize:
  807.                 /* Fire up a separate dialog for world geometry. */
  808.                 world_shape_dialog();
  809.                 break;
  810.             case diVariantsRealTime:
  811.                 /* Fire up a separate dialog for real time setup. */
  812.                 real_time_dialog();
  813.                 break;
  814.             case diVariantsMoreVariants:
  815.                 more_variants_dialog();
  816.                 break;
  817.             default:
  818.                 /* Handle all the checkboxes similarly. */
  819.                 if (between(diVariantsFirstCheckBox, ditem, diVariantsFirstCheckBox+MAXCHECKBOXES-1)) {
  820.                     GetDItem(variants_win, ditem, &itemtype, &itemhandle, &itemrect);
  821.                     toggle_checkbox(itemhandle);
  822.                 }
  823.                 break;
  824.         }
  825.     }
  826.     DisposDialog(variants_win);
  827.     variants_win = nil;
  828.     return changed;
  829. }
  830.  
  831. static void
  832. set_variant_item(di, flag, flag2, val)
  833. int di;
  834. int flag, flag2, val;
  835. {
  836.     short itemtype;  Handle itemhandle;  Rect itemrect;
  837.  
  838.     GetDItem(variants_win, di, &itemtype, &itemhandle, &itemrect);
  839.     ShowControl((ControlHandle) itemhandle);
  840.     HiliteControl((ControlHandle) itemhandle, (flag ? 0 : 255));
  841.     if (flag && flag2) {
  842.         SetCtlValue((ControlHandle) itemhandle, val);
  843.     }
  844. }
  845.  
  846. /* Go through all the game's variants and set up appropriate flags. */
  847.  
  848. static void
  849. interpret_variants()
  850. {
  851.     int i;
  852.     char *vartypename;
  853.     Obj *vartmp;
  854.     Variant *var;
  855.  
  856.     any_variants = FALSE;
  857.     vary_world_seen = vary_see_all = vary_sequential = FALSE;
  858.     vary_world = vary_real_time = FALSE;
  859.     if (selected_game == NULL || selected_game->variants == NULL)
  860.       return;
  861.     numcheckboxes = 0;
  862.     numsliders = 0;
  863.     for (i = 0; selected_game->variants[i].id != lispnil; ++i) {
  864.         var = &(selected_game->variants[i]);
  865.         any_variants = TRUE;
  866.         vartypename = c_string(var->id);
  867.         switch (keyword_code(vartypename)) {
  868.             case K_WORLD_SEEN:
  869.                 vary_world_seen = TRUE;
  870.                 new_seen = FALSE;
  871.                 if (var->dflt != lispnil) {
  872.                     vartmp = eval(var->dflt);
  873.                     if (numberp(vartmp)) {
  874.                         new_seen = c_number(vartmp);
  875.                     }
  876.                 }
  877.                 break;
  878.             case K_SEE_ALL:
  879.                 vary_see_all = TRUE;
  880.                 new_seeall = FALSE;
  881.                 if (var->dflt != lispnil) {
  882.                     vartmp = eval(var->dflt);
  883.                     if (numberp(vartmp)) {
  884.                         new_seeall = c_number(vartmp);
  885.                     }
  886.                 }
  887.                 break;
  888.             case K_SEQUENTIAL:
  889.                 vary_sequential = TRUE;
  890.                 new_sequential = FALSE;
  891.                 if (var->dflt != lispnil) {
  892.                     vartmp = eval(var->dflt);
  893.                     if (numberp(vartmp)) {
  894.                         new_sequential = c_number(vartmp);
  895.                     }
  896.                 }
  897.                 break;
  898.             case K_WORLD_SIZE:
  899.                 vary_world = TRUE;
  900.                 /* Start with some defaults. */
  901.                 new_circumference = DEFAULTCIRCUMFERENCE;
  902.                 new_width = DEFAULTWIDTH;  new_height = DEFAULTHEIGHT;
  903.                 new_latitude = 0;  new_longitude = 0;
  904.                 /* If we have explicit defaults, use them. */
  905.                 if (var->dflt != lispnil) {
  906.                     vartmp = var->dflt;
  907.                     new_width = c_number(eval(car(vartmp)));
  908.                     vartmp = cdr(vartmp);
  909.                     if (vartmp != lispnil) {
  910.                         new_height = c_number(eval(car(vartmp)));
  911.                     } else {
  912.                         new_height = new_width;
  913.                     }
  914.                     /* (should also seed other values if given) */
  915.                 }
  916.                 break;
  917.             case K_REAL_TIME:
  918.                 vary_real_time = TRUE;
  919.                 /* Start with some defaults. */
  920.                 new_time_for_game = new_time_per_side = new_time_per_turn = 0;
  921.                 /* If we have explicit defaults, use them. */
  922.                 if (var->dflt != lispnil) {
  923.                     vartmp = var->dflt;
  924.                     new_time_for_game = c_number(eval(car(vartmp)));
  925.                     vartmp = cdr(vartmp);
  926.                     if (vartmp != lispnil) {
  927.                         new_time_per_side = c_number(eval(car(vartmp)));
  928.                     } else {
  929.                         new_time_per_side = 0;
  930.                     }
  931.                     vartmp = cdr(vartmp);
  932.                     if (vartmp != lispnil) {
  933.                         new_time_per_turn = c_number(eval(car(vartmp)));
  934.                     } else {
  935.                         new_time_per_turn = 0;
  936.                     }
  937.                 }
  938.                 break;
  939.             default:
  940.                 if (1 /* boolean variant */) {
  941.                     if (numcheckboxes >= MAXCHECKBOXES) {
  942.                         init_warning("too many variants, can't set all of them");
  943.                         break;
  944.                     }
  945.                     checkboxes[numcheckboxes++] = var;
  946.                 } else {
  947.                     /* (should set up slider) */
  948.                 }
  949.                 break;
  950.         }
  951.     }
  952. }
  953.  
  954. /* This is where we actually change the state of the game according to the variants. */
  955.  
  956. static void
  957. implement_variants()
  958. {
  959.     int i;
  960.     short itemtype;  Handle itemhandle;  Rect itemrect;
  961.  
  962.     variants = lispnil;
  963.     if (vary_world_seen) {
  964.         GetDItem(variants_win, diVariantsWorldSeen, &itemtype, &itemhandle, &itemrect);
  965.         push_key_int_binding(&variants, K_WORLD_SEEN, GetCtlValue((ControlHandle) itemhandle));
  966.     }
  967.     if (vary_see_all) {
  968.         GetDItem(variants_win, diVariantsSeeAll, &itemtype, &itemhandle, &itemrect);
  969.         push_key_int_binding(&variants, K_SEE_ALL, GetCtlValue((ControlHandle) itemhandle));
  970.     }
  971.     if (vary_sequential) {
  972.         GetDItem(variants_win, diVariantsSequential, &itemtype, &itemhandle, &itemrect);
  973.         push_key_int_binding(&variants, K_SEQUENTIAL, GetCtlValue((ControlHandle) itemhandle));
  974.     }
  975.     if (vary_world) {
  976.         /* It is critically important that users not be able to reshape already-alloced
  977.            areas, but do let them know that their request had to be overridden. */
  978.         if (((area.width > 0 && area.width != new_width)
  979.             || (area.height > 0 && area.height != new_height)
  980.             || (world.circumference > 0 && world.circumference != new_circumference))
  981.             && (1 /* some layers (probably) allocated already */)) {
  982.             /* (this is misleading, is an "expected" alert) */
  983.             init_warning("Area dimensions must remain %d x %d, %d around world",
  984.                          area.width, area.height, world.circumference);
  985.             new_width = area.width;  new_height = area.height;
  986.             new_circumference = world.circumference;
  987.         }
  988.         /* Make a world-size-setting and glue it into the list of variants. */
  989.         push_key_cdr_binding(&variants, K_WORLD_SIZE, 
  990.                               cons(new_number(new_width),
  991.                                    cons(new_number(new_height),
  992.                                        cons(new_number(new_circumference),
  993.                                             cons(new_number(new_longitude),
  994.                                                  cons(new_number(new_latitude),
  995.                                               lispnil))))));
  996.     }
  997.     if (vary_real_time) {
  998.         push_key_cdr_binding(&variants, K_REAL_TIME, 
  999.                          cons(new_number(new_time_for_game),
  1000.                           cons(new_number(new_time_per_side),
  1001.                            cons(new_number(new_time_per_turn),
  1002.                             lispnil))));
  1003.     }
  1004.     /* Implement the random checkbox variants. */
  1005.     for (i = 0; i < numcheckboxes; ++i) {
  1006.         GetDItem(variants_win, diVariantsFirstCheckBox + i, &itemtype, &itemhandle, &itemrect);
  1007.         push_int_binding(&variants, checkboxes[i]->id, GetCtlValue((ControlHandle) itemhandle));
  1008.     }
  1009.     /* (should implement the random slider variants) */
  1010.     do_module_variants(selected_game, variants);
  1011.     /* Recheck everything, the variants might have broken something. */
  1012.     if (any_variants) {
  1013.         check_game_validity();
  1014.     }
  1015. }
  1016.  
  1017. /* Specially adapted version of xform. */
  1018.  
  1019. static void
  1020. special_xform(int x, int y, int *sxp, int *syp)
  1021. {
  1022.     *sxp = (x + y / 2 - (new_height / 4)) / new_scale;
  1023.     *syp = (new_height - y) / new_scale;
  1024. }
  1025.  
  1026.  
  1027. /* This is a callback that draws the world and its areas. */
  1028.  
  1029. /* (Would be really cool to draw accurate curvature over globe here) */
  1030.  
  1031. static pascal void
  1032. draw_world_picture(WindowPtr win, short ditem)
  1033. {
  1034.     int llx, lly, lrx, lry, rx, ry, urx, ury, ulx, uly, lx, ly;
  1035.     Point dims, center, center1, center2;
  1036.     Rect worldrect, hemirect1, hemirect2, arearect;
  1037.     PolyHandle poly;
  1038.     RgnHandle two_hemi_rgn, tmprgn;
  1039.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1040.  
  1041.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1042.     /* Make a framed gray background for the world picture. */
  1043.     FillRect(&itemrect, QDPat(gray));
  1044.     FrameRect(&itemrect);
  1045.     /* Alter the itemrect so as to leave a bit of border. */
  1046.     InsetRect(&itemrect, 5, 5);
  1047.     dims.h = itemrect.right - itemrect.left;  dims.v = itemrect.bottom - itemrect.top;
  1048.     center.h = itemrect.left + dims.h / 2;  center.v = itemrect.top + dims.v / 2;
  1049.     worldrect = itemrect;
  1050.     new_scale = 1;
  1051.     if (new_circumference > dims.h || new_width > dims.h)
  1052.       new_scale = 2;
  1053.     if (new_circumference > 2 * dims.h || new_width > 2 * dims.h)
  1054.       new_scale = 4;
  1055.     /* Draw the whole world as a circle if it's not too big. */
  1056.     if (new_circumference < new_width) {
  1057.         /* If world circumference smaller than area, world is not being used at all. */
  1058.     } else {
  1059.         hemirect1.left = center.h - (new_circumference / new_scale) / 2;
  1060.         hemirect1.right = center.h;
  1061.         hemirect1.top = center.v - ((new_circumference / new_scale) / 2) / 2;
  1062.         hemirect1.bottom = center.v + ((new_circumference / new_scale) / 2) / 2;
  1063.         center1.h = hemirect1.left + (hemirect1.right - hemirect1.left) / 2;
  1064.         center1.v = hemirect1.top + (hemirect1.bottom - hemirect1.top) / 2;
  1065.         EraseOval(&hemirect1);
  1066.         FrameOval(&hemirect1);
  1067.         hemirect2 = hemirect1;
  1068.         OffsetRect(&hemirect2, (new_circumference / new_scale) / 2, 0);
  1069.         center2.h = hemirect2.left + (hemirect2.right - hemirect2.left) / 2;
  1070.         center2.v = hemirect2.top + (hemirect2.bottom - hemirect2.top) / 2;
  1071.         EraseOval(&hemirect2);
  1072.         FrameOval(&hemirect2);
  1073.         two_hemi_rgn = NewRgn();
  1074.         OpenRgn();
  1075.         FrameOval(&hemirect1);
  1076.         FrameOval(&hemirect2);
  1077.         CloseRgn(two_hemi_rgn);
  1078.         /* Draw some grid lines. */
  1079.         tmprgn = NewRgn();
  1080.         GetClip(tmprgn);
  1081.         SetClip(two_hemi_rgn);
  1082.         /* Latitude lines. */
  1083.         MoveTo(hemirect1.left,  center1.v);
  1084.         LineTo(hemirect1.right, center1.v);
  1085.         MoveTo(hemirect1.left,  center1.v + ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1086.         LineTo(hemirect1.right, center1.v + ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1087.         MoveTo(hemirect1.left,  center1.v + (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1088.         LineTo(hemirect1.right, center1.v + (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1089.         MoveTo(hemirect1.left,  center1.v - ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1090.         LineTo(hemirect1.right, center1.v - ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1091.         MoveTo(hemirect1.left,  center1.v - (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1092.         LineTo(hemirect1.right, center1.v - (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1093.         MoveTo(hemirect2.left,  center2.v);
  1094.         LineTo(hemirect2.right, center2.v);
  1095.         MoveTo(hemirect2.left,  center2.v + ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1096.         LineTo(hemirect2.right, center2.v + ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1097.         MoveTo(hemirect2.left,  center2.v + (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1098.         LineTo(hemirect2.right, center2.v + (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1099.         MoveTo(hemirect2.left,  center2.v - ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1100.         LineTo(hemirect2.right, center2.v - ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1101.         MoveTo(hemirect2.left,  center2.v - (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1102.         LineTo(hemirect2.right, center2.v - (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1103.         /* Longitude lines. */
  1104.         MoveTo(center1.h, hemirect1.top);
  1105.         LineTo(center1.h, hemirect1.bottom);
  1106.         MoveTo(center2.h, hemirect2.top);
  1107.         LineTo(center2.h, hemirect2.bottom);
  1108.         SetClip(tmprgn);
  1109.         DisposeRgn(tmprgn);
  1110.     }
  1111.     /* should use pair of world disks as clipping region */
  1112.     if (new_width == new_circumference) {
  1113.         arearect.top = center.v - (new_height / new_scale) / 2;
  1114.         arearect.left = center.h - (new_width / new_scale) / 2;
  1115.         arearect.bottom = center.v + (new_height / new_scale) / 2;
  1116.         arearect.right = center.h + (new_width / new_scale) / 2;
  1117.         OffsetRect(&arearect, (new_latitude / new_scale), (new_longitude / new_scale));
  1118.         FillRect(&arearect, QDPat(dkGray));
  1119.     } else {
  1120.         /* Assume that area to be drawn is a hexagon. */
  1121.         poly = OpenPoly();        
  1122.         special_xform(0 + new_height/2, 0, &llx, &lly);
  1123.         MoveTo(llx, lly);
  1124.         special_xform(new_width-1, 0, &lrx, &lry);
  1125.          LineTo(lrx, lry);
  1126.         special_xform(new_width-1, new_height/2, &rx, &ry);
  1127.         LineTo(rx, ry);
  1128.          special_xform(new_width-1 - new_height/2, new_height-1, &urx, &ury);
  1129.         LineTo(urx, ury);
  1130.          special_xform(0, new_height-1, &ulx, &uly);
  1131.         LineTo(ulx, uly);
  1132.          special_xform(0, new_height/2, &lx, &ly);
  1133.         LineTo(lx, ly);
  1134.         LineTo(llx, lly);
  1135.         ClosePoly();
  1136.         OffsetPoly(poly, center.h - (new_width / 2) / new_scale, center.v - (new_height / 2) / new_scale);
  1137.         OffsetPoly(poly, (new_latitude / new_scale), (new_longitude / new_scale));
  1138.         FillPoly(poly, QDPat(dkGray));
  1139.     }
  1140. }
  1141.  
  1142. static pascal Boolean
  1143. filter_world_setup(DialogPtr dialog, EventRecord *evt, short *itemhit)
  1144. {
  1145.     GrafPtr oldport;
  1146.     Point pt, dims, center;
  1147.     short ditem;
  1148.     int wid, hgt;
  1149.     char ch;
  1150.     Str255 tmpstr;
  1151.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1152.     WindowPtr win = world_shape_win;
  1153.  
  1154.     switch (evt->what) {
  1155.         case mouseDown:
  1156.             GetPort(&oldport);
  1157.             SetPort(dialog);
  1158.             pt = evt->where;
  1159.             GlobalToLocal(&pt);
  1160.             ditem = FindDItem(win, pt) + 1;
  1161.             if (ditem == diWorldShapePicture) {
  1162.                 GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1163.                 dims.h = itemrect.right - itemrect.left;  dims.v = itemrect.bottom - itemrect.top;
  1164.                 center.h = itemrect.left + dims.h / 2;  center.v = itemrect.top + dims.v / 2;
  1165.                 /* Compute width and height so that click was on a corner. */
  1166.                 wid = (2 * abs(pt.h - center.h) + abs(pt.v - center.v)) * new_scale;
  1167.                 hgt = (2 * abs(pt.v - center.v)) * new_scale;
  1168.                 /* The following test should always pass. */
  1169.                 if (valid_area_shape(wid, hgt, FALSE)) {
  1170.                     put_number_into_ditem(diWorldShapeWidth, wid);
  1171.                     put_number_into_ditem(diWorldShapeHeight, hgt);
  1172.                     /* The outer dialog loop will do the redraw. */
  1173.                 }
  1174.                 SetPort(oldport);
  1175.                 *itemhit = diWorldShapePicture;
  1176.                 return TRUE;
  1177.             } else {
  1178.                 SetPort(oldport);
  1179.             }
  1180.             break;
  1181.         case keyDown:
  1182.             ch = evt->message & charCodeMask;
  1183.             if (ch == 3 || ch == 13) {
  1184.                 *itemhit = diWorldShapeOK;
  1185.                 return TRUE;
  1186.             }
  1187.             break;
  1188.     }
  1189.     return FALSE;
  1190. }
  1191.  
  1192. /* This dialog asks for the shape and size of a world to generate randomly. */
  1193.  
  1194. static int
  1195. world_shape_dialog()
  1196. {
  1197.     int done = FALSE, maybechanged = TRUE, changed = FALSE;
  1198.     Str255 tmpstr;
  1199.     short ditem;
  1200.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1201.     WindowPtr win;
  1202.  
  1203.     if (world_shape_win == nil) {
  1204.         world_shape_win = GetNewDialog(dWorldShape, NULL, (DialogPtr) -1L);
  1205.         GetDItem(world_shape_win, diWorldShapePicture, &itemtype, &itemhandle, &itemrect);
  1206.         SetDItem(world_shape_win, diWorldShapePicture, itemtype, (Handle) draw_world_picture, &itemrect);
  1207.         /* Plug all the starting values into dialog items. */
  1208.         win = world_shape_win;
  1209.         put_number_into_ditem(diWorldShapeCircumference, new_circumference);
  1210.         put_number_into_ditem(diWorldShapeWidth, new_width);
  1211.         put_number_into_ditem(diWorldShapeHeight, new_height);
  1212.         put_number_into_ditem(diWorldShapeLatitude, new_latitude);
  1213.         put_number_into_ditem(diWorldShapeLongitude, new_longitude);
  1214.     }
  1215.     while (!done) {
  1216.         /* adjust items to reflect current status */
  1217.         if (maybechanged) {
  1218.             /* Dig the values out of the dialog boxes. */
  1219.             win = world_shape_win;
  1220.             get_number_from_ditem(diWorldShapeCircumference, new_circumference);
  1221.             get_number_from_ditem(diWorldShapeWidth, new_width);
  1222.             get_number_from_ditem(diWorldShapeHeight, new_height);
  1223.             get_number_from_ditem(diWorldShapeLatitude, new_latitude);
  1224.             get_number_from_ditem(diWorldShapeLongitude, new_longitude);
  1225.             GetDItem(world_shape_win, diWorldShapeIcon, &itemtype, &itemhandle, &itemrect);
  1226.             SetDItem(world_shape_win, diWorldShapeIcon, itemtype,
  1227.                      (new_circumference == new_width ? cylinder_icon : hexagon_icon), &itemrect);
  1228.             DrawDialog(world_shape_win); /* just to do picture item */
  1229.             maybechanged = FALSE;
  1230.         }
  1231.         /* If the proposed shape is valid, enable the OK button. */
  1232.         /* (still need to disable returns etc) */
  1233.         GetDItem(world_shape_win, diWorldShapeOK, &itemtype, &itemhandle, &itemrect);
  1234.         HiliteControl((ControlHandle) itemhandle,
  1235.                       (valid_area_shape(new_width, new_height, FALSE) ? 0 : 255));
  1236.         draw_default_button(world_shape_win, diWorldShapeOK);
  1237.         ModalDialog((ModalFilterProcPtr) filter_world_setup, &ditem);
  1238.         switch (ditem) {
  1239.             case diWorldShapeOK:
  1240.                 changed = TRUE;
  1241.                 /* Fall through to next case. */
  1242.             case diWorldShapeCancel:
  1243.                 done = TRUE;
  1244.                 break;
  1245.             case diWorldShapePicture:
  1246.             case diWorldShapeCircumference:
  1247.             case diWorldShapeWidth:
  1248.             case diWorldShapeHeight:
  1249.             case diWorldShapeLatitude:
  1250.             case diWorldShapeLongitude:
  1251.                 maybechanged = TRUE;
  1252.                 break;
  1253.             case diWorldShapeIcon:
  1254.                 if (new_circumference != new_width)
  1255.                   new_circumference = new_width;
  1256.                 else
  1257.                   new_circumference = 360;
  1258.                 maybechanged = TRUE;
  1259.                 break;
  1260.             default:
  1261.                 break;
  1262.         }
  1263.     }
  1264.     DisposDialog(world_shape_win);
  1265.     world_shape_win = nil;
  1266.     return changed;
  1267. }
  1268.  
  1269. /* This dialog asks for real time parameters of the game. */
  1270.  
  1271. static int
  1272. real_time_dialog()
  1273. {
  1274.     int done = FALSE, maybechanged = TRUE, changed = FALSE;
  1275.     Str255 tmpstr;
  1276.     short ditem;
  1277.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1278.     WindowPtr win;
  1279.  
  1280.     if (real_time_win == nil) {
  1281.         real_time_win = GetNewDialog(dRealTime, NULL, (DialogPtr) -1L);
  1282.         /* Plug all the starting values into dialog items. */
  1283.         win = real_time_win;
  1284.         put_number_into_ditem(diRealTimeForGame, new_time_for_game);
  1285.         put_number_into_ditem(diRealTimePerSide, new_time_per_side);
  1286.         put_number_into_ditem(diRealTimePerTurn, new_time_per_turn);
  1287.     }
  1288.     while (!done) {
  1289.         /* adjust items to reflect current status */
  1290.         if (maybechanged) {
  1291.             /* Dig the values out of the dialog boxes. */
  1292.             win = real_time_win;
  1293.             get_time_from_ditem(diRealTimeForGame, new_time_for_game);
  1294.             get_time_from_ditem(diRealTimePerSide, new_time_per_side);
  1295.             get_time_from_ditem(diRealTimePerTurn, new_time_per_turn);
  1296.             maybechanged = FALSE;
  1297.         }
  1298.         /* If the proposed timings are valid, enable the OK button. */
  1299.         /* (still need to disable returns etc) */
  1300.         GetDItem(real_time_win, diRealTimeOK, &itemtype, &itemhandle, &itemrect);
  1301.         HiliteControl((ControlHandle) itemhandle,
  1302.                       (1 ? 0 : 255));
  1303.         draw_default_button(real_time_win, diRealTimeOK);
  1304.         ModalDialog(NULL, &ditem);
  1305.         switch (ditem) {
  1306.             case diRealTimeOK:
  1307.                 changed = TRUE;
  1308.                 /* Fall through to next case. */
  1309.             case diRealTimeCancel:
  1310.                 done = TRUE;
  1311.                 break;
  1312.             case diRealTimeForGame:
  1313.             case diRealTimePerSide:
  1314.             case diRealTimePerTurn:
  1315.                 maybechanged = TRUE;
  1316.                 break;
  1317.             default:
  1318.                 break;
  1319.         }
  1320.     }
  1321.     DisposDialog(real_time_win);
  1322.     real_time_win = nil;
  1323.     return changed;
  1324. }
  1325.  
  1326. static int
  1327. more_variants_dialog()
  1328. {
  1329.     int done = FALSE, maybechanged = TRUE, changed = FALSE;
  1330.     char *gamename;
  1331.     Str255 tmpstr;
  1332.     short ditem;
  1333.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1334.  
  1335.     if (more_variants_win == nil) {
  1336.         more_variants_win = GetNewDialog(dMoreVariants, NULL, (DialogPtr) -1L);
  1337.         gamename = (selected_game->title ? selected_game->title : selected_game->name);
  1338.         sprintf(spbuf, "More Variants for \"%s\"", gamename);
  1339.         c2p(spbuf, tmpstr);
  1340.         GetDItem(variants_win, diVariantsText, &itemtype, &itemhandle, &itemrect);
  1341.         SetIText(itemhandle, tmpstr);
  1342.     }
  1343.     while (!done) {
  1344.         /* adjust items to reflect current status */
  1345.         if (maybechanged) {
  1346.             /* Dig the values out of the dialog boxes. */
  1347.             maybechanged = FALSE;
  1348.         }
  1349.         draw_default_button(more_variants_win, diMoreVariantsOK);
  1350.         ModalDialog(NULL, &ditem);
  1351.         switch (ditem) {
  1352.             case diMoreVariantsOK:
  1353.                 changed = TRUE;
  1354.                 /* Fall through to next case. */
  1355.             case diMoreVariantsCancel:
  1356.                 done = TRUE;
  1357.                 break;
  1358.             default:
  1359.                 break;
  1360.         }
  1361.     }
  1362.     DisposDialog(more_variants_win);
  1363.     more_variants_win = nil;
  1364.     return changed;
  1365. }
  1366.  
  1367. /* This is a callback that displays the list of sides and their assigned players. */
  1368.  
  1369. pascal void
  1370. draw_player_setup_list(WindowPtr win, short ditem)
  1371. {
  1372.     int i, advantage = -1;
  1373.     Rect entryrect, tmprect;
  1374.     char *sidetitle, playertitle[BUFSIZE];
  1375.     Player *player;
  1376.     Side *side;
  1377.     RgnHandle tmprgn;
  1378.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1379.  
  1380.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1381.     tmprgn = NewRgn();
  1382.     GetClip(tmprgn);
  1383.     ClipRect(&itemrect);
  1384.     entryrect = itemrect;
  1385.     entryrect.bottom = entryrect.top + playerh;
  1386.     InsetRect(&entryrect, 1, 1);
  1387.     for (i = 0; i < numsides; ++i) {
  1388.         /* Clear this entry. */
  1389.         tmprect = entryrect;
  1390.         InsetRect(&tmprect, -1, -1);
  1391.         EraseRect(&tmprect);
  1392.         /* Describe the side. */
  1393.         side = assignments[i].side;
  1394.         draw_side_emblem(playersetupwin, entryrect.left + 2, entryrect.top + 2,
  1395.                          16, 16, side_number(side), shadow_emblem);
  1396.         sidetitle = short_side_title(side);
  1397.         if (empty_string(sidetitle))
  1398.           sidetitle = "(unnamed)";
  1399.         MoveTo(entryrect.left + 2 + 16 + 2, entryrect.top + playerbaseline);
  1400.         TextFont(newYork);
  1401.         TextFace(0);
  1402.         DrawText(sidetitle, 0, strlen(sidetitle));
  1403.         /* Only draw other info for sides "in" this game. */
  1404.         if (side->ingame) {
  1405.             /* Describe the intended player for the side. */
  1406.             player = assignments[i].player;
  1407.             long_player_title(playertitle, player, "mac");
  1408.             MoveTo(entryrect.left + 2 + 130, entryrect.top + playerbaseline);
  1409.             TextFace(italic);
  1410.             DrawText(playertitle, 0, strlen(playertitle));
  1411.             /* Indicate the player's initial advantage. */
  1412.             sprintf(spbuf, "%d", player->advantage);
  1413.             MoveTo(entryrect.left + 2 + 260, entryrect.top + playerbaseline);
  1414.             TextFace(0);
  1415.             DrawText(spbuf, 0, strlen(spbuf));
  1416.             if (player->password) {
  1417.                 MoveTo(entryrect.right - 15, entryrect.top + playerbaseline);
  1418.                 DrawText((strcmp(player->password, "x") == 0 ? "r" : "R"), 0, 1);
  1419.             }
  1420.         }
  1421.         /* (should be able to draw/update selection separately) */
  1422.         if (i == selectedplayer) {
  1423.             tmprect = entryrect;
  1424.             InsetRect(&tmprect, -1, -1);
  1425.             InvertRect(&tmprect);
  1426.             InsetRect(&tmprect, 3, 3);
  1427.             InvertRect(&tmprect);
  1428.         } else {
  1429.             /* Sides may be present, but not in the game; gray them out. */
  1430.             if (!side->ingame)
  1431.               gray_out_rect(&entryrect);
  1432.             FrameRect(&entryrect);
  1433.         }
  1434.         OffsetRect(&entryrect, 0, playerh);
  1435.     }
  1436.     /* Draw the remaining available positions as gray outlines. */
  1437.     PenPat(QDPat(gray));
  1438.     for (i = numsides + 1; i < g_sides_max(); ++i) {
  1439.         FrameRect(&entryrect);
  1440.         OffsetRect(&entryrect, 0, playerh);
  1441.     }
  1442.     PenNormal();
  1443.     SetClip(tmprgn);
  1444.     DisposeRgn(tmprgn);
  1445. }
  1446.  
  1447. /* Callback to draw the little up/down arrow. */
  1448.  
  1449. pascal void
  1450. draw_player_setup_advantage(WindowPtr win, short ditem)
  1451. {
  1452.     int advantage, halfhgt;
  1453.     Side *side;
  1454.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1455.  
  1456.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1457.     EraseRect(&itemrect);
  1458.     if (updownpicture != nil) {
  1459.         DrawPicture(updownpicture, &itemrect);
  1460.         /* Never gray out the border, just the arrows inside. */
  1461.         InsetRect(&itemrect, 1, 1);
  1462.         if (!selected_a_player()) {
  1463.             gray_out_rect(&itemrect);
  1464.         } else {
  1465.             advantage = selected_player()->advantage;
  1466.             side = selected_side();
  1467.             halfhgt = (itemrect.bottom - itemrect.top) / 2;
  1468.             if (advantage <= side->minadvantage) {
  1469.                 /* Gray out the down arrow. */
  1470.                 itemrect.top += halfhgt;
  1471.                 gray_out_rect(&itemrect);
  1472.                 itemrect.top -= halfhgt;
  1473.             }
  1474.             if (side->maxadvantage > 0 && advantage >= side->maxadvantage) {
  1475.                 /* Gray out the up arrow. */
  1476.                 itemrect.bottom -= halfhgt;
  1477.                 gray_out_rect(&itemrect);
  1478.                 itemrect.bottom += halfhgt;
  1479.             }
  1480.         }
  1481.     }
  1482. }
  1483.  
  1484. static int
  1485. adjust_advantage(Player *player, Side *side, int amt)
  1486. {
  1487.     int newadvantage;
  1488.  
  1489.     if (player == NULL)
  1490.       return FALSE;
  1491.     newadvantage = player->advantage + amt;
  1492.     if (amt > 0 && side->maxadvantage > 0 && newadvantage > side->maxadvantage) {
  1493.         beep();
  1494.         return FALSE;
  1495.     }
  1496.     if (amt < 0 && newadvantage < side->minadvantage) {
  1497.         beep();
  1498.         return FALSE;
  1499.     }
  1500.     if (newadvantage != player->advantage) {
  1501.         player->advantage = newadvantage;
  1502.         return TRUE;
  1503.     } else
  1504.       return FALSE;
  1505. }
  1506.  
  1507. void
  1508. select_player(int n)
  1509. {
  1510.     if ((!between(0, n, numsides-1))
  1511.         || (assignments[n].side && !(assignments[n].side->ingame))) {
  1512.         n = -1;
  1513.     }
  1514.     selectedplayer = n;
  1515.     /* (update ditem directly?) */
  1516. }
  1517.  
  1518. /* Bring up a dialog that allows rearrangement of the side/player assignments.
  1519.    This is a movable modal because we might have to be here a while waiting
  1520.    for remote players to join in. */
  1521.  
  1522. int
  1523. player_setup_dialog()
  1524. {
  1525.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1526.  
  1527.     /* Create the window. */
  1528.     if (playersetupwin == nil) {
  1529.         playersetupwin = GetNewDialog(dPlayerSetup, NULL, (DialogPtr) -1L);
  1530.         GetDItem(playersetupwin, diPlayerSetupList, &itemtype, &itemhandle, &itemrect);
  1531.         SetDItem(playersetupwin, diPlayerSetupList, itemtype, (Handle) draw_player_setup_list, &itemrect);
  1532.         GetDItem(playersetupwin, diPlayerSetupAdvantage, &itemtype, &itemhandle, &itemrect);
  1533.         SetDItem(playersetupwin, diPlayerSetupAdvantage, itemtype, (Handle) draw_player_setup_advantage, &itemrect);
  1534.         updownpicture = (PicHandle) GetResource('PICT', pUpDownPicture);
  1535.     }
  1536.     /* Always start out with the first player selected. */
  1537.     selectedplayer = 0;
  1538.     set_player_setup_button_states();
  1539.     draw_default_button(playersetupwin, diPlayerSetupOK);
  1540.     ShowWindow(playersetupwin);
  1541.     return TRUE;
  1542. }
  1543.  
  1544. /* Set the state of the various buttons and controls.  In general, a side/player
  1545.    pair must be selected before anything can be done. */    
  1546.  
  1547. void
  1548. set_player_setup_button_states()
  1549. {
  1550.     int locked = g_player_sides_locked();
  1551.     int player = (selected_a_player() && !locked);
  1552.     int i, numingame = 0, numremotewaiting = 0;
  1553.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1554.  
  1555.     for (i = 0; i < numsides; ++i) {
  1556.         if (assignments[i].side && (assignments[i].side)->ingame) {
  1557.             ++numingame;
  1558.             if ((assignments[i].player)->password) {
  1559.                 if (strcmp((assignments[i].player)->password, "x") == 0)
  1560.                   ++numremotewaiting;
  1561.             }
  1562.         }
  1563.     }
  1564.  
  1565. #define set_player_button(di, val)  \
  1566.     GetDItem(playersetupwin, (di), &itemtype, &itemhandle, &itemrect);  \
  1567.     SetDItem(playersetupwin, (di), ((itemtype & 127) | ((val) ? 0 : 128)), itemhandle, &itemrect);  \
  1568.     HiliteControl((ControlHandle) itemhandle, ((val) ? 0 : 255));
  1569.  
  1570.     set_player_button(diPlayerSetupOK, numremotewaiting == 0);
  1571.     set_player_button(diPlayerSetupAdd, !locked && (numsides < g_sides_max()));
  1572.     set_player_button(diPlayerSetupRemove, player && !locked && (numsides > g_sides_min()));
  1573.     set_player_button(diPlayerSetupRename, player && !(selected_side()->nameslocked));
  1574.     set_player_button(diPlayerSetupAI, player);
  1575.     set_player_button(diPlayerSetupRemote, player);
  1576.     set_player_button(diPlayerSetupExchange, player && numingame > 1);
  1577. }
  1578.  
  1579. /* Handle any event that we don't want DialogSelect to get. */
  1580.  
  1581. void
  1582. handle_player_setup_event(EventRecord *event)
  1583. {
  1584.     char ch;
  1585.     Point mouse;
  1586.  
  1587.     switch (event->what) {
  1588.         case keyDown:
  1589.         case autoKey:
  1590.             ch = event->message & charCodeMask;
  1591.             /* Check for menukey equivalents. */
  1592.             if (event->modifiers & cmdKey) {
  1593.                 if (event->what == keyDown) {
  1594.                     if (ch == '.') {
  1595.                         /* (should) Cancel out of player setup. */
  1596.                         beep();
  1597.                     }
  1598.                 }
  1599.             } else {
  1600.                 if (event->what == keyDown) {
  1601.                     if (ch == 3 || ch == 13) {
  1602.                         /* The mouse position doesn't actually get used here. */
  1603.                         mouse.h = mouse.v = 0;
  1604.                         if (hit_player_setup_dialog(diPlayerSetupOK, mouse))
  1605.                             launch_game_2();
  1606.                     }
  1607.                     /* Shortcuts to tweak the advantage. */
  1608.                     else if (ch == '+') {
  1609.                         adjust_advantage(selected_player(), selected_side(), 1);
  1610.                     } else if (ch == '-') {
  1611.                         adjust_advantage(selected_player(), selected_side(), -1);
  1612.                     }
  1613.                 }
  1614.             }
  1615.             break;
  1616.         case kHighLevelEvent:
  1617.             AEProcessAppleEvent(event);
  1618.             break;
  1619.     }
  1620. }
  1621.  
  1622. int
  1623. hit_player_setup_dialog(int ditem, Point pt)
  1624. {
  1625.     int playersok = FALSE, done = FALSE, changedselected = FALSE, changedall = FALSE;
  1626.     Player *tmpplayer;
  1627.     Side *tmpside;
  1628.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1629.  
  1630.     tmpplayer = selected_player();
  1631.     tmpside = selected_side();
  1632.  
  1633.     switch (ditem) {
  1634.         case diPlayerSetupOK:
  1635.             playersok = TRUE;
  1636.             /* Fall through. */
  1637.         case diPlayerSetupCancel:
  1638.             done = TRUE;
  1639.             break;
  1640.         case diPlayerSetupAdd:
  1641.             add_side_and_player();
  1642.             /* Select the newly-created side/player pair. */
  1643.             select_player(numsides - 1);
  1644.             init_emblem_images(selected_side());
  1645.             changedall = TRUE;
  1646.             break;
  1647.         case diPlayerSetupRemove:
  1648.             remove_side_and_player();
  1649.             break;
  1650.         case diPlayerSetupRename:
  1651.             tmpside->name = NULL;
  1652.             tmpside->noun = NULL;
  1653.             /* always need to clear this cache before renaming... */
  1654.             tmpside->pluralnoun = NULL;
  1655.             tmpside->adjective = NULL;
  1656.             make_up_side_name(tmpside);
  1657.             init_emblem_images(tmpside);
  1658.             changedselected = TRUE;
  1659.             break;
  1660.         case diPlayerSetupAI:
  1661.             /* Click between mplayer and not, for now */
  1662.             if (tmpplayer->aitypename) {
  1663.                 tmpplayer->aitypename = NULL;
  1664.             } else {
  1665.                 tmpplayer->aitypename = "mplayer";
  1666.             }
  1667.             changedselected = TRUE;
  1668.             break;
  1669.         case diPlayerSetupRemote:
  1670.             /* Toggle whether to wait for human to connect in */
  1671.             /* (should not use password as flag...) */
  1672.             if (tmpplayer->password) {
  1673.                 tmpplayer->password = NULL;
  1674.             } else {
  1675.                 tmpplayer->password = "x";
  1676.             }
  1677.             changedselected = TRUE;
  1678.             break;
  1679.         case diPlayerSetupExchange:
  1680.             /* Make the next player in the exchange be the selected one,
  1681.                then multiple clicks on the exchange button will move the
  1682.                selected player down through the list. */
  1683.             selectedplayer = exchange_players(selectedplayer, -1);
  1684.             changedall = TRUE;
  1685.             break;
  1686.         case diPlayerSetupList:
  1687.             GetDItem(playersetupwin, diPlayerSetupList, &itemtype, &itemhandle, &itemrect);
  1688.             select_player((pt.v - itemrect.top) / playerh);
  1689.             if (tmpplayer != selected_player())
  1690.               changedselected = TRUE;
  1691.             break;
  1692.         case diPlayerSetupAdvantage:
  1693.             GetDItem(playersetupwin, diPlayerSetupAdvantage, &itemtype, &itemhandle, &itemrect);
  1694.             if (tmpplayer != NULL) {
  1695.                 if (pt.v - itemrect.top < ((itemrect.bottom - itemrect.top) / 2)) {
  1696.                     changedselected = adjust_advantage(tmpplayer, tmpside, 1);
  1697.                 } else {
  1698.                     changedselected = adjust_advantage(tmpplayer, tmpside, -1);
  1699.                 }
  1700.             }
  1701.             break;
  1702.         default:
  1703.             break;
  1704.     }
  1705.     if (changedall || changedselected) {
  1706.         set_player_setup_button_states();
  1707.         /* (should minimize redrawing) */
  1708.         DrawDialog(playersetupwin);
  1709.         draw_default_button(playersetupwin, diPlayerSetupOK);
  1710.     }
  1711.     if (done) {
  1712.         /* Get rid of this dialog, unlikely to need it again. */
  1713.         DisposDialog(playersetupwin);
  1714.         playersetupwin = nil;
  1715.     }
  1716.     return playersok;
  1717. }
  1718.  
  1719. void
  1720. add_remote_player(char *name)
  1721. {
  1722.     int i;
  1723.  
  1724.     if (name == NULL)
  1725.       name = "somebody";
  1726.     for (i = 0; i < numsides; ++i) {
  1727.         if (assignments[i].side && (assignments[i].side)->ingame) {
  1728.             if ((assignments[i].player)->password && strcmp((assignments[i].player)->password, "x") == 0) {
  1729.                   (assignments[i].player)->password = name;
  1730.                   return;
  1731.             }
  1732.         }
  1733.     }
  1734.     /* No open positions available (should alert?) */
  1735.     beep();
  1736. }
  1737.  
  1738. /* This dialog opens a player-specified file as a game. */
  1739.  
  1740. int
  1741. open_game_dialog()
  1742. {
  1743.     Point pnt;
  1744.     SFTypeList typelist;
  1745.     SFReply reply;
  1746.  
  1747.     /* Gotta be somewhere... */
  1748.     SetPt(&pnt, 100, 100);
  1749.     /* Game module are text files. */
  1750.     typelist[0] = 'TEXT';
  1751.     SFGetFile(pnt, "\p", NULL, 1, typelist, NULL, &reply);
  1752.     if (reply.good) {
  1753.         return open_game_from_name_and_volume(reply.fName, reply.vRefNum);
  1754.     }
  1755.     return FALSE;
  1756. }
  1757.  
  1758. /* Open a game file using an FSSpec, which will typically come from an AppleEvent. */
  1759.  
  1760. int
  1761. open_game_from_fsspec(FSSpec *fsspec)
  1762. {
  1763.     short vrefnum;
  1764.  
  1765.     /* (should check the return result) */
  1766.     OpenWD(fsspec->vRefNum, fsspec->parID, XconqSignature, &vrefnum);
  1767.     return open_game_from_name_and_volume(fsspec->name, vrefnum);
  1768. }
  1769.  
  1770. /* Given a filename and volume number, load, check, and launch. */
  1771.  
  1772. int
  1773. open_game_from_name_and_volume(Str255 name, int vrefnum)
  1774. {
  1775.     char modulename[256];
  1776.     Module *module;
  1777.  
  1778.     /* Set the current volume to be where the file is, so path-less
  1779.        fopen() in module loading below will find the file.  Note that this
  1780.        won't work for loading multiple modules from multiple non-library
  1781.        locations, but this dialog can only load one file at a time anyway. */
  1782.     SetVol(nil, vrefnum);
  1783.     clear_game_modules();
  1784.     /* Build a module with the given filename. */
  1785.     module = get_game_module(NULL);
  1786.     p2c(name, modulename);
  1787.     module->filename = copy_string(modulename);
  1788.     module->hook = NULL;
  1789.     /* This module is effectively the "selected game", whether or not
  1790.        it's loadable. */
  1791.     selected_game = module;
  1792.     /* Load the module. */
  1793.     mainmodule = module;
  1794.     load_game_module(module, TRUE);
  1795.     /* Change cursor back, in case it was different during loading. */
  1796.     SetCursor(&QD(arrow));
  1797.     if (!module->loaded) {
  1798.         init_warning("Could not load a module from %s", module->filename);
  1799.         return FALSE;
  1800.     }
  1801.     /* If the loaded module has bugs, we will get alerts here. */
  1802.     check_game_validity();
  1803.     /* We can now join up with the "new game" startup sequence. */
  1804.     return launch_game();
  1805. }
  1806.  
  1807. /* This is just the collection of init steps shared by both new and open dialogs.
  1808.    If the player doesn't cancel out of anything, then the game will be running when
  1809.    this routine returns. */
  1810.  
  1811. int
  1812. launch_game()
  1813. {
  1814.     /* Bring up the pre-player-setup variants. */
  1815.     if (!variants_dialog()) {
  1816.         /* Cancelled from variants dialog, go back to wherever. */
  1817.         return FALSE;
  1818.     }
  1819.     /* Load any colors first; we can use them to substitute for patterns. */
  1820.     init_terrain_colors();
  1821.     /* Set up the images we'll be needing. */
  1822.     init_terrain_images();
  1823.     init_unit_images();
  1824.     /* Trial side/player assignment, will create the default player if nececessary. */
  1825.     make_trial_assignments();
  1826.     /* Get the emblems so they can be displayed in the player dialog. */
  1827.     init_all_emblem_images();
  1828.     /* Ask for modifications to the trial assignment. */
  1829.     return player_setup_dialog();
  1830. }
  1831.  
  1832. void
  1833. launch_game_2()
  1834. {
  1835.     /* Complain if any images were missing.  Will also note if image resource files
  1836.        were never found - might be one of the explanations. */
  1837.     check_for_missing_images();
  1838.     /* A last-minute patch to ensure reasonable area dimensions. */
  1839.     if (area.width == 0)
  1840.       area.width = DEFAULTWIDTH;
  1841.     if (area.height == 0)
  1842.       area.height = DEFAULTHEIGHT;
  1843.     set_area_shape(area.width, area.height, TRUE);
  1844.     /* About to do lengthy init calcs, show the progress bar. */
  1845.     open_progress_dialog();
  1846.     /* Synthesize the remainder of the initial game setup. */
  1847.     calculate_globals();
  1848.     run_synth_methods();
  1849.     final_init();
  1850.     assign_players_to_sides();
  1851.     /* No more interminable init calcs (we hope), so close the progress bar. */
  1852.     close_progress_dialog();
  1853.     /* Init the display now. */
  1854.     init_display();
  1855.     /* Do first set of turn calcs. */
  1856.     run_game(0);
  1857. }
  1858.  
  1859. /* This counts up all the images that had to be manufactured, and maybe puts up an
  1860.    alert asking if the player wants to go with the substitutes. */
  1861.  
  1862. void
  1863. check_for_missing_images()
  1864. {
  1865.     int u, t, e;
  1866.     Side *side;
  1867.     Str255 tmpstr;
  1868.  
  1869.     for_all_terrain_types(t) {
  1870.         if (timages[t]->ersatz && !t_color_defined(t))
  1871.           record_missing_image(TTYP, t_type_name(t));
  1872.     }
  1873.     for_all_unit_types(u) {
  1874.         if (uimages[u]->ersatz)
  1875.           record_missing_image(UTYP, u_type_name(u));
  1876.     }
  1877.     for_all_sides(side) {
  1878.         e = side_number(side);
  1879.         if (eimages[e]->ersatz)
  1880.           record_missing_image(3, side_desig(side)); 
  1881.     }
  1882.     if (missing_images(tmpbuf)) {
  1883.         c2p(tmpbuf, tmpstr);
  1884.         ParamText(tmpstr, (foundimagesfile ? "\p" : "\p & no image resource files found"), "\p", "\p");
  1885.         switch (CautionAlert(aImagesMissing, nil)) {
  1886.             case 1:
  1887.                 /* We decided to live with low-quality images. */
  1888.                 break;
  1889.             case 2:
  1890.                 /* Ugliness is unbearable, get out now. */
  1891.                 ExitToShell();
  1892.                 break;
  1893.         }
  1894.     }
  1895. }
  1896.  
  1897. /* Rotate around "marching parentheses" cursors to indicate reading is happening. */
  1898.  
  1899. void
  1900. announce_read_progress()
  1901. {
  1902.     if (readprogressors[curcurs] != nil)
  1903.       SetCursor(*(readprogressors[curcurs]));
  1904.     curcurs = (curcurs + 1) % NUMcParens;
  1905. }
  1906.  
  1907. /* Display progress in lengthy initialization processes. */
  1908.  
  1909. /* This is the app-defined item that actually draws the progress bar. */
  1910.  
  1911. pascal void
  1912. draw_progress(WindowPtr win, short ditem)
  1913. {
  1914.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1915.  
  1916.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1917.     if (progress >= 0) {
  1918.         /* Only draw the gray bg initially or when "progress" decreases (would be a
  1919.            bug in synth method, but useful to get visual evidence if it happens). */
  1920.         if (lastprogress < 0 || lastprogress > progress) {
  1921.             FillRect(&itemrect, QDPat(gray));
  1922.             FrameRect(&itemrect);
  1923.         }
  1924.         itemrect.right =
  1925.             itemrect.left + (progress * (itemrect.right - itemrect.left)) / 100;
  1926.         FillRect(&itemrect, QDPat(black));
  1927.     } else {
  1928.         EraseRect(&itemrect);
  1929.     }
  1930. }
  1931.  
  1932. /* This starts off the lengthy process. */
  1933.  
  1934. void
  1935. open_progress_dialog()
  1936. {
  1937.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1938.  
  1939.     progresswin = GetNewDialog(dProgress, NULL, (DialogPtr) -1L);
  1940.     GetDItem(progresswin, diProgressBar, &itemtype, &itemhandle, &itemrect);
  1941.     SetDItem(progresswin, diProgressBar, itemtype, (Handle) draw_progress, &itemrect);
  1942. #if 0
  1943.     /* Always disable the Stop button for now. */
  1944.     /* (should figure out what to do with this) */
  1945.     GetDItem(progresswin, diProgressCancel, &itemtype, &itemhandle, &itemrect);
  1946.     HiliteControl((ControlHandle) itemhandle, 255);
  1947. #endif
  1948.     curcurs = 0;
  1949.     if (progressors[curcurs] != nil)
  1950.       SetCursor(*(progressors[curcurs]));
  1951. }
  1952.  
  1953. /* This is called to announce the beginning of a lengthy process. */
  1954.  
  1955. void
  1956. announce_lengthy_process(char *msg)
  1957. {
  1958.     Str255 tmpstr;
  1959.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1960.  
  1961.     if (progresswin == nil)
  1962.       return;
  1963.     /* Paste in the description of what is going to take so long. */
  1964.     GetDItem(progresswin, diProgressText, &itemtype, &itemhandle, &itemrect);
  1965.     c2p(msg, tmpstr);
  1966.     SetIText(itemhandle, tmpstr);
  1967.     /* Initialize the progress bar itself. */
  1968.     progress = -1;
  1969.     lastprogress = 0;
  1970.     ShowWindow(progresswin);
  1971.     SelectWindow(progresswin);
  1972.     Dprintf("%s;", msg);
  1973. #ifdef USE_CONSOLE
  1974.     if (Debug) fflush(stdout);
  1975. #endif
  1976.     curcurs = 0;
  1977.     if (progressors[curcurs] != nil)
  1978.       SetCursor(*(progressors[curcurs]));
  1979. }
  1980.  
  1981. /* This is called periodically to say how much progress has been made. */
  1982.  
  1983. void
  1984. announce_progress(int pctdone)
  1985. {
  1986.     if (progresswin == nil)
  1987.       return;
  1988.     /* (should notice that stop button was hit and use to escape from program) */
  1989.     SelectWindow(progresswin);
  1990.     lastprogress = progress;
  1991.     progress = min(max(0, pctdone), 100);
  1992.     /* Redraw the progress bar app-defined item, but only if there was
  1993.        any change to report. */
  1994.     /* (should restrict to bar alone, otherwise flickers a lot) */
  1995.     if (progress != lastprogress)
  1996.       DrawDialog(progresswin);
  1997.     /* Switch to the next cursor in the animation sequence. */
  1998.     if (progressors[curcurs] != nil)
  1999.       SetCursor(*(progressors[curcurs]));
  2000.     curcurs = (curcurs + 1) % NUMcSynth;
  2001.     Dprintf(" %d%%", pctdone);
  2002. #ifdef USE_CONSOLE
  2003.     if (Debug) fflush(stdout);
  2004. #endif
  2005. }
  2006.  
  2007. /* This is called to signal the end of the lengthy process. */
  2008.  
  2009. void
  2010. finish_lengthy_process()
  2011. {
  2012.     short itemtype;  Handle itemhandle;  Rect itemrect;
  2013.  
  2014.     if (progresswin == nil)
  2015.       return;
  2016.     SelectWindow(progresswin);
  2017.     /* Replace the text with a "reassuring" but uninformative message. */
  2018.     GetDItem(progresswin, diProgressText, &itemtype, &itemhandle, &itemrect);
  2019.     SetIText(itemhandle, "\pKeeping busy...");
  2020.     progress = -1;
  2021.     /* Force a redraw, which will cause the progress bar to be erased. */
  2022.     DrawDialog(progresswin);
  2023.     Dprintf(" done.\n");
  2024.     curcurs = 0;
  2025.     if (progressors[curcurs] != nil)
  2026.       SetCursor(*(progressors[curcurs]));
  2027. }
  2028.  
  2029. /* When we're completely done with all lengthy init processes, discard the dialog. */
  2030.  
  2031. void
  2032. close_progress_dialog()
  2033. {
  2034.     /* Restore the normal cursor. */
  2035.     SetCursor(&QD(arrow));
  2036.     DisposDialog(progresswin);
  2037.     progresswin = nil;
  2038. }
  2039.  
  2040. /* Kernel callback to put in a default player, assumed to be on
  2041.    "this" screen (which we call "mac", for lack of inspiration). */
  2042.  
  2043. Player *
  2044. add_default_player()
  2045. {
  2046.     Player *player = add_player();
  2047.     
  2048.     player->displayname = "mac";
  2049.     return player;
  2050. }
  2051.  
  2052. /* This is a kernel callback that sets the given side to have a user
  2053.    interface.  On the Mac, we just record the side as `dside', will
  2054.    finish opening later. */
  2055.  
  2056. void
  2057. init_ui(Side *side)
  2058. {
  2059.     if (side != NULL) {
  2060.         side->ui = (UI *) xmalloc(sizeof(UI));
  2061.         side->ui->active = FALSE;
  2062.         dside = side;
  2063.         /* Make sure this is allocated.  Note that xmalloc clears, so this
  2064.            will look like it has an empty Pascal string. */
  2065.         if (curdatestr == NULL)
  2066.           curdatestr = (char *) xmalloc(100);
  2067.     }
  2068. }
  2069.  
  2070. /* Flag the screen as officially "open", init random globals, and open the
  2071.    initial set of windows. */
  2072.  
  2073. void
  2074. init_display()
  2075. {
  2076.     int p;
  2077.     Handle handle;
  2078.  
  2079.     /* If dside never got assigned, try some hasty repairs. */
  2080.     if (dside == NULL) {
  2081.         init_warning("Nobody wanted a display!");
  2082.         /* Make an arbitrary choice - pick the first side. */
  2083.         init_ui(side_n(1));
  2084.         if (dside == NULL) {
  2085.             /* Geez, nothing is going right. */
  2086.             init_error("Can't set up the display!");
  2087.         }
  2088.     }
  2089.     if (bordbitmaps == NULL) {
  2090.         bordbitmaps = (BitMap *) xmalloc(NUMPOWERS * sizeof(BitMap));
  2091.     }
  2092.     if (connbitmaps == NULL) {
  2093.         connbitmaps = (BitMap *) xmalloc(NUMPOWERS * sizeof(BitMap));
  2094.     }
  2095.     /* Init polygon-caching machinery. */
  2096.     for (p = 0; p < NUMPOWERS; ++p) {
  2097.         polygons[p] = nil;
  2098.         lastpolyx[p] = lastpolyy[p] = 0;
  2099.         cellrgns[p] = gridcellrgns[p] = nil;
  2100.         lastcellrgnx[p] = lastcellrgny[p] = 0;
  2101.         lastgridcellrgnx[p] = lastgridcellrgny[p] = 0;
  2102.         cellrgns30[p] = gridcellrgns30[p] = nil;
  2103.         lastcellrgnx30[p] = lastcellrgny30[p] = 0;
  2104.         lastgridcellrgnx30[p] = lastgridcellrgny30[p] = 0;
  2105.         cellrgns15[p] = gridcellrgns15[p] = nil;
  2106.         lastcellrgnx15[p] = lastcellrgny15[p] = 0;
  2107.         lastgridcellrgnx15[p] = lastgridcellrgny15[p] = 0;
  2108.         bordbitmaps[p].rowBytes = 0;
  2109.         connbitmaps[p].rowBytes = 0; 
  2110.     }
  2111.     handle = GetResource('ICN#', 140);
  2112.     if (handle != nil) {
  2113.         HLock(handle);
  2114.         bordbitmaps[4].baseAddr = *handle;
  2115.         bordbitmaps[4].rowBytes = 4;
  2116.         SetRect(&(bordbitmaps[4].bounds), 0, 0, 32, 32);
  2117.     }
  2118.     handle = GetResource('ICN#', 141);
  2119.     if (handle != nil) {
  2120.         HLock(handle);
  2121.         connbitmaps[4].baseAddr = *handle;
  2122.         connbitmaps[4].rowBytes = 4;
  2123.         SetRect(&(connbitmaps[4].bounds), 0, 0, 32, 32);
  2124.     }
  2125.     /* Init assorted list heads and window pointers. */
  2126.     maplist = NULL;
  2127.     listlist = NULL;
  2128.     unitcloseuplist = NULL;
  2129.     historywin = nil;
  2130.     /* Flag the interface as ready to display stuff.  After this point,
  2131.        any events causing display activity will actually be rendered
  2132.        on the screen. */
  2133.     dside->ui->active = TRUE;
  2134.     /* Now bring up the initial collection of windows. */
  2135.     if (0 /* should restore preferred windows */) {
  2136.     } else {
  2137.         /* Indicate that this is the first time these windows are being
  2138.            shown; (should) adjust to fit the screen size. */
  2139.         first_windows = TRUE;
  2140.         /* The game window is always useful. */
  2141.         create_game_window();
  2142.         ShowWindow(gamewin);
  2143.         /* This will be the main playing display, so it should be up. */
  2144.         create_map(5);
  2145.         /* This needs to come up so that players unfamiliar with the game
  2146.            will have some guidance. */
  2147.         instructions_dialog();
  2148.         first_windows = FALSE;
  2149.     }
  2150. #ifdef DESIGNERS
  2151.     /* If we're designing, bring up the palette automatically. */
  2152.     if (dside->designer) {
  2153.         create_design_window();
  2154.     }
  2155. #endif /* DESIGNERS */
  2156.     Dprintf("Opened Mac display!\n");
  2157. }
  2158.  
  2159. /* Allocate and fill in images for all the unit types. */
  2160.  
  2161. void
  2162. init_unit_images()
  2163. {
  2164.     int u;
  2165.  
  2166.     uimages = (ImageFamily **) xmalloc(numutypes * sizeof(ImageFamily *));
  2167.  
  2168.     for_all_unit_types(u) {
  2169.         uimages[u] = get_unit_type_images(dside, u, mac_interp_imf, mac_load_imf,
  2170.                                           add_default_unit_image);
  2171.     }
  2172. }
  2173.  
  2174. /* The default unit image is a box with the name of the unit inside. */
  2175.  
  2176. static void
  2177. add_default_unit_image(ImageFamily *imf, int u, char *name)
  2178. {
  2179.     Rect tmprect;
  2180.     PicHandle pichandle;
  2181.     RgnHandle tmprgn;
  2182.     Image *img;
  2183.     MacImage *macimg;
  2184.  
  2185.     img = get_img(imf, 32, 32);
  2186.     if (img == NULL)
  2187.       return;
  2188.     img->istile = FALSE;
  2189.     macimg = get_mac_image(img);
  2190.     tmprgn = NewRgn();
  2191.     GetClip(tmprgn);
  2192.     SetRect(&tmprect, 0, 0, 32, 32);
  2193.     ClipRect(&tmprect);
  2194.     pichandle = OpenPicture(&tmprect);
  2195.     InsetRect(&tmprect, 2, 2);
  2196.     FillRect(&tmprect, QDPat(white));
  2197.     InsetRect(&tmprect, 1, 1);
  2198.     FrameRect(&tmprect);
  2199.     MoveTo(4, 20);
  2200.     DrawText(name, 0, strlen(name));
  2201.     ClosePicture();
  2202.     SetClip(tmprgn);
  2203.     DisposeRgn(tmprgn);
  2204.     macimg->monopict = pichandle;
  2205. }
  2206.  
  2207. /* Allocate space and load a named color for each terrain type
  2208.    that defines one. */
  2209.  
  2210. void
  2211. init_terrain_colors()
  2212. {
  2213.     int t;
  2214.  
  2215.     if (hasColorQD) {
  2216.         tcolors = (ImageColor **) xmalloc(numttypes * sizeof(ImageColor *));
  2217.         for_all_terrain_types(t) {
  2218.             if (!empty_string(t_color(t))) {
  2219.                 tcolors[t] = new_image_color(t_color(t));
  2220.                 mac_load_image_color(tcolors[t]);
  2221.             }
  2222.         }
  2223.     }
  2224.     interp_named_color(g_grid_color(), &gridgray, &gridcolor);
  2225.     interp_named_color(g_unseen_color(), &unseengray, &unseencolor);
  2226.     /* If the grid color and the unseen terrain color are the same, then we can
  2227.        draw more efficiently. */
  2228.     if (strcmp(g_grid_color(), g_unseen_color()) == 0
  2229.         && empty_string(g_unseen_image_name()))
  2230.       grid_matches_unseen = TRUE;
  2231.     blackcolor.red = blackcolor.green = blackcolor.blue = 0;
  2232. }
  2233.  
  2234. /* Set up the collection of terrain images/patterns, making a default pattern
  2235.    if necessary. */
  2236.  
  2237. void
  2238. init_terrain_images()
  2239. {
  2240.     int t;
  2241.  
  2242.     timages = (ImageFamily **) xmalloc(numttypes * sizeof(ImageFamily *));
  2243.  
  2244.     for_all_terrain_types(t) {
  2245.         timages[t] = get_terrain_type_images(dside, t, mac_interp_imf, mac_load_imf,
  2246.                                              add_default_terrain_image);
  2247.     }
  2248.     get_unseen_images(dside, mac_interp_imf, mac_load_imf, NULL);
  2249. }
  2250.  
  2251. /* Synthesize a terrain pattern, using the type to make a distinguishing
  2252.    pattern. Basically the bits of the type number make a sort of bar code,
  2253.    but enclosed in a box and with a horizontal bar through the middle, so
  2254.    as to make it easier to tell which is which on a map.  (Trust me, I've
  2255.    already experimented with this.) */
  2256.  
  2257. static void
  2258. add_default_terrain_image(ImageFamily *imf, int t, char *name)
  2259. {
  2260.     Image *img;
  2261.     MacImage *macimg;
  2262.  
  2263.     img = get_img(imf, 8, 8);
  2264.     if (img == NULL)
  2265.       return;
  2266.     img->minw = img->minh = 1;
  2267.     img->maxw = img->maxh = 9999;
  2268.     img->istile = TRUE;
  2269.     macimg = get_mac_image(img);
  2270.     SET_IMG_PAT (macimg, 0, 0x01);
  2271.     SET_IMG_PAT (macimg, 1, (t << 2) + 0x01);
  2272.     SET_IMG_PAT (macimg, 2, (t << 2) + 0x01);
  2273.     SET_IMG_PAT (macimg, 3, 0x7d);
  2274.     SET_IMG_PAT (macimg, 4, (t << 2) + 0x01);
  2275.     SET_IMG_PAT (macimg, 5, (t << 2) + 0x01);
  2276.     SET_IMG_PAT (macimg, 6, 0x01);
  2277.     SET_IMG_PAT (macimg, 7, 0xff);
  2278.     macimg->patdefined = 1;
  2279.     /* Count this type as having only a default pattern, but only if we can't
  2280.        possibly render the terrain with a solid color. */
  2281.     if (!(hasColorQD && t_color_defined(t)))
  2282.       imf->ersatz = TRUE;
  2283. }
  2284.  
  2285. /* Given a color name, get actual RGB values for it.  As a special case for b/w
  2286.    screens, the five shades of gray also get set here. */
  2287.  
  2288. void
  2289. interp_named_color(char *name, enum grays *grayvar, RGBColor *colorvar)
  2290. {
  2291.     ImageColor tmpcolor;
  2292.  
  2293.     if (strcmp(name, "black") == 0) {
  2294.         *grayvar = blackgray;
  2295.         colorvar->red = colorvar->green = colorvar->blue = 0;
  2296.     } else if (strcmp(name, "dark gray") == 0) {
  2297.         *grayvar = darkgray;
  2298.         colorvar->red = colorvar->green = colorvar->blue = 16384;
  2299.     } else if (strcmp(name, "gray") == 0) {
  2300.         *grayvar = mediumgray;
  2301.         colorvar->red = colorvar->green = colorvar->blue = 32768;
  2302.     } else if (strcmp(name, "light gray") == 0) {
  2303.         *grayvar = lightgray;
  2304.         colorvar->red = colorvar->green = colorvar->blue = 49000;
  2305.     } else if (strcmp(name, "white") == 0) {
  2306.         *grayvar = whitegray;
  2307.         colorvar->red = colorvar->green = colorvar->blue = 65535;
  2308.     } else if (hasColorQD) {
  2309.         tmpcolor.name = name;
  2310.         mac_load_image_color(&tmpcolor);
  2311.         colorvar->red = tmpcolor.r;
  2312.         colorvar->green = tmpcolor.g;
  2313.         colorvar->blue = tmpcolor.b;
  2314.     }
  2315. }
  2316.  
  2317. /* Load/create all the images of each side's emblem. */
  2318.  
  2319. void
  2320. init_all_emblem_images()
  2321. {
  2322.     Side *side2;
  2323.  
  2324.     eimages = (ImageFamily **) xmalloc((MAXSIDES + 1) * sizeof(ImageFamily *));
  2325.  
  2326.     for_all_sides_plus_indep(side2) {
  2327.         init_emblem_images(side2);
  2328.     }
  2329. }
  2330.  
  2331. /* Set up the emblems for a particular side.  This is used both to do all sides
  2332.    initially and whenever a side changes emblems. */
  2333.  
  2334. void
  2335. init_emblem_images(Side *side2)
  2336. {
  2337.     int e;
  2338.  
  2339.     e = side_number(side2);
  2340.     eimages[e] = get_emblem_images(dside, side2, mac_interp_imf, mac_load_imf,
  2341.                                    add_default_emblem_image);
  2342. }
  2343.  
  2344. /* Default image is SICN-sized number.  Use only if no images have been found
  2345.    and the emblem name is not "none". */
  2346.  
  2347. static void
  2348. add_default_emblem_image(ImageFamily *imf, int e, char *name)
  2349. {
  2350.     int i;
  2351.     char tmpbuf[10];
  2352.     Str255 tmpstr;
  2353.     Handle sicnhandle, maskhandle;
  2354.     Rect tmprect;
  2355.     PicHandle pichandle;
  2356.     RgnHandle tmprgn;
  2357.     Image *img;
  2358.     MacImage *macimg;
  2359.  
  2360.     img = get_img(imf, 16, 16);
  2361.     img->minw = img->minh = 8;
  2362.     img->maxw = img->maxh = 64;
  2363.     /* Emblems never tile. */
  2364.     img->istile = FALSE;
  2365.     macimg = get_mac_image(img);
  2366.     sprintf(tmpbuf, "s%d", e);
  2367.     c2p(name, tmpstr);
  2368.     sicnhandle = GetNamedResource('SICN', tmpstr);
  2369.     if (tmpstr != nil) {
  2370.         /* Found a sicn with the side's number in it. */
  2371.         /* Image itself is the first 32 bytes, mask is second 32 if present. */
  2372.         macimg->monosicn = sicnhandle;
  2373.         maskhandle = (Handle) NewHandle(32);
  2374.         if (SizeResource(sicnhandle) >= 64) {
  2375.             for (i = 0; i < 32; ++i) {
  2376.                 (*maskhandle)[i] = (*sicnhandle)[i+32];
  2377.             }
  2378.         } else {
  2379.             /* Set up an all-white background. */
  2380.             for (i = 0; i < 32; ++i) {
  2381.                 (*maskhandle)[i] = 0xff;
  2382.             }
  2383.         }
  2384.         macimg->masksicn = maskhandle;
  2385.     } else {
  2386.         /* If we couldn't even find a number sicn, build a crude substitute pict. */
  2387.         imf->ersatz = TRUE;
  2388.         tmprgn = NewRgn();
  2389.         GetClip(tmprgn);
  2390.         SetRect(&tmprect, 0, 0, 16, 16);
  2391.         ClipRect(&tmprect);
  2392.         pichandle = OpenPicture(&tmprect);
  2393.         FillRect(&tmprect, QDPat(white));
  2394.         MoveTo(2, 12);
  2395.         DrawText(tmpbuf+1, 0, strlen(tmpbuf+1));
  2396.         ClosePicture();
  2397.         SetClip(tmprgn);
  2398.         DisposeRgn(tmprgn);
  2399.         macimg->monopict = pichandle;
  2400.     }
  2401. }
  2402.